/*******************************************************************************
* Purpose: Preprocess 36-month HOME instrument
* 
* Dependencies:
*   Input: DSC0097.dta, 36-Months HOME assessment
*   Output: HOME_46m.dta
*
* Notes: -
*******************************************************************************/
set more off
clear all

global storage: env storage

use "$storage/ece_parenting/raw_data/DSC0001.dta"
keep IHDP PAG
sort IHDP
merge 1:1 IHDP using "$storage/ece_parenting/raw_data/DSC0097.dta", nogenerate

// keep primary sample
keep if PAG == 1

// drop all observation where at least one response is missing
foreach var of varlist V1-V55 {
  drop if missing(`var') 
}

// order variables 
order IHDP V*, sequential

// construct subscales
gen alt_subscale_1_36 = (V1 + V2 + V3 + V4 + V5 + V6 + V11 + V12 + V33 + ///
						 V34 + V35 + V36 + V37 + V43) / 14 
gen alt_subscale_2_36 = (V7 + V8 + V9 + V10 + V13) / 5
gen alt_subscale_3_36 = (V15 + V48) / 2
gen alt_subscale_4_36 = (V16 + V17 + V26 + V27 + V28 + V29 + V30 + V31 + ///
						 V32) / 9
gen alt_subscale_5_36 = (V19 + V20 + V22) / 3
gen alt_subscale_6_36 = (V21 + V23 + V24 + V25) / 4
gen alt_subscale_7_36 = (V44 + V45 + V46) / 3
gen alt_subscale_8_36 = (V52 + V53 + V54) / 3

label variable alt_subscale_1_36 "Learning Stimulation"
label variable alt_subscale_2_36 "Access to Reading"
label variable alt_subscale_3_36 "Parental Verbal Skills"
label variable alt_subscale_4_36 "Parental Warmth"
label variable alt_subscale_5_36 "Home Exterior"
label variable alt_subscale_6_36 "Home Interior"
label variable alt_subscale_7_36 "Outings/Activities"
label variable alt_subscale_8_36 "Parental Lack of Hostility"

// total score
egen alt_parenting_36_raw = rowtotal(V1 V2 V3 V4 V5 V6 V11 V12 ///
						V33 V34 V35 V36 V37 V43 V7 V8 V9 V10 V13 V15 V48 ///
						V16 V17 V26 V27 V28 V29 V30 V31 V32 V19 V20 V22 ///
						V21 V23 V24 V25 V44 V45 V46 V52 V53 V54)

// drop indicator variables
drop V*

// construct factor
factor alt_subscale_1_36 alt_subscale_2_36 alt_subscale_3_36  /// 
	   alt_subscale_4_36 alt_subscale_5_36 alt_subscale_6_36 ///
	   alt_subscale_7_36 alt_subscale_8_36
predict parenting_36_alt

// save
capture mkdir "$storage/ece_parenting/intermediate_data/HOME"
save "$storage/ece_parenting/intermediate_data/HOME/HOME_36m.dta", replace
